// 声明 server
import StaticServer from 'react-native-static-server';
import RNFS from 'react-native-fs';
import {isAndroid} from './device';
import {DeviceEventEmitter} from 'react-native';

const path = RNFS.DocumentDirectoryPath + '/games';

class LiveServer {
  startServer = async () => {
    return new Promise((resolve, reject) => {
      this.server = new StaticServer(8080, path, {
        localOnly: true,
        keepAlive: true,
      });

      this.server
        .start()
        .then((url) => {
          console.log('static Server running', url);
          resolve();
        })
        .catch((err) => {
          reject(err);
        });
    });
  };

  reStartServer = async () => {
    await this.server.stop();
    await this.startServer();
  };
}

const liveServer = new LiveServer();
liveServer.startServer(); // 启动

export default liveServer;
